package com.project.pushtask.schedule;


import com.project.pushtask.dto.ParamDto;
import com.project.pushtask.entity.*;
import com.project.pushtask.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * - 定时任务
 */
@Slf4j
@Component
@EnableScheduling
public class Scheduler {

    @Resource
    CheliangService cheliangService;
    @Resource
    JinchuzhanService jinchuzhanService;
    @Resource
    LudanService ludanService;
    @Resource
    RfidService rfidService;
    @Resource
    ShikeService shikeService;
    @Resource
    SijiService sijiService;
    @Resource
    XianluService xianluService;
    @Resource
    YouhaoService youhaoService;
    @Resource
    ZhanjiService zhanjiService;
    @Resource
    DailyService dailyService;

    //    @Scheduled(cron = "0 0 1 * * ?")
    private void syncJob() {
        log.info("每天1点定时任务执行开始");
        try {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate now = LocalDate.now();
            LocalDate before = now.minusDays(2);
            LocalDate LastMonth = now.minusDays(30);
            ParamDto dto = new ParamDto();
            dto.setStartDate(formatter.format(before));
            dto.setEndDate(formatter.format(before));
            LocalDate before2 = now.minusDays(3);
            ParamDto dto2 = new ParamDto();
            dto2.setStartDate(formatter.format(before2));
            dto2.setEndDate(formatter.format(before2));
            ParamDto dtoExpire = new ParamDto();
            dtoExpire.setStartDate(formatter.format(LastMonth));

            log.info("推送【t_xianlu】" + "【" + formatter.format(before) + "】");
            List<Xianlu> xianluList = xianluService.getList(dto);
            if (xianluList.size() > 0) {
                xianluService.saveList(xianluList);
                xianluService.deleteExpireDate(dtoExpire);
            }
            log.info("【t_xianlu】" + "数量:" + xianluList.size());

            log.info("推送【t_zhanji】" + "【" + formatter.format(before) + "】");
            List<Zhanji> zhanjiList = zhanjiService.getList(dto);
            if (zhanjiList.size() > 0) {
                zhanjiService.saveList(zhanjiList);
                zhanjiService.deleteExpireDate(dtoExpire);
            }
            log.info("【t_zhanji】" + "数量:" + zhanjiList.size());

            log.info("推送【t_cheliang】" + "【" + formatter.format(before2) + "】");
            List<Cheliang> cheliangList = cheliangService.getList(dto2);
            if (cheliangList.size() > 0) {
                cheliangService.saveList(cheliangList);
                cheliangService.deleteExpireDate(dtoExpire);
            }
            log.info("【t_cheliang】" + "数量:" + cheliangList.size());

            log.info("推送【t_siji】" + "【" + formatter.format(before) + "】");
            List<Siji> sijiList = sijiService.getList(dto);
            if (sijiList.size() > 0) {
                sijiService.saveList(sijiList);
                sijiService.deleteExpireDate(dtoExpire);
            }
            log.info("【t_siji】" + "数量:" + sijiList.size());

            log.info("推送【t_ludan】" + "【" + formatter.format(before2) + "】");
            List<Ludan> ludanList = ludanService.getList(dto2);
            if (ludanList.size() > 0) {
                ludanService.saveList(ludanList);
                ludanService.deleteExpireDate(dtoExpire);
            }
            log.info("【t_ludan】" + "数量:" + ludanList.size());

            log.info("推送【t_youhao】" + "【" + formatter.format(before2) + "】");
            List<Youhao> youhaoList = youhaoService.getList(dto2);
            if (youhaoList.size() > 0) {
                youhaoService.saveList(youhaoList);
                youhaoService.deleteExpireDate(dtoExpire);
            }
            log.info("【t_youhao】" + "数量:" + youhaoList.size());

            log.info("推送【t_shike】" + "【" + formatter.format(before2) + "】");
            List<Shike> shikeList = shikeService.getList(dto2);
            if (shikeList.size() > 0) {
                shikeService.saveList(shikeList);
                shikeService.deleteExpireDate(dtoExpire);
            }
            log.info("【t_shike】" + "数量:" + shikeList.size());

            log.info("推送【t_daily】" + "【" + formatter.format(before2) + "】");
            List<Daily> dailyList = dailyService.getList(dto2);
            if (dailyList.size() > 0) {
                dailyService.saveList(dailyList);
                dailyService.deleteExpireDate(dtoExpire);
            }
            log.info("【t_daily】" + "数量:" + dailyList.size());

            log.info("推送【t_rfid】" + "【" + formatter.format(before2) + "】");
            List<Rfid> rfidList = rfidService.getList(dto2);
            if (rfidList.size() > 0) {
                rfidService.saveList(rfidList);
                rfidService.deleteExpireDate(dtoExpire);
            }
            log.info("【t_rfid】" + "数量:" + rfidList.size());

        } catch (Exception e) {
            log.error("【每天1点定时任务执行结束,执行失败：【{}】", e.getMessage());
        } finally {
            log.info("每天1点定时任务执行结束");
        }
    }

    //    @Scheduled(cron = "0/30 * * * * ?")
    private void syncJob2() {
        log.info("定时任务每30秒执行开始");
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            log.info("推送【[t_jinchuzhan】" + "【" + sdf.format(new Date()) + "】");
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate now = LocalDate.now();
            ParamDto dto = new ParamDto();
            dto.setStartDate(formatter.format(now));
            dto.setEndDate(formatter.format(now));
            List<Jinchuzhan> jinchuzhanList = jinchuzhanService.getListByFlag(dto);
            log.info("【t_jinchuzhan】" + "数量:" + jinchuzhanList.size());
            if (jinchuzhanList.size() > 0) {
                jinchuzhanService.saveList(jinchuzhanList);
                jinchuzhanService.deleteAll();
            }
        } catch (Exception e) {
            log.error("【定时任务每30秒执行结束,执行失败：【{}】", e.getMessage());
        } finally {
            log.info("定时任务每30秒执行结束");
        }
    }

    //    @Scheduled(cron = "0 0 22 * * ?")
    private void syncCreateJob() {
        log.info("每天22点定时任务执行开始");
        try {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate now = LocalDate.now();

            log.info("【t_xianlu】" + "【" + formatter.format(now) + "】");
            List<Xianlu> xianluList = xianluService.getCreateList();
            if (xianluList.size() > 0) {
                xianluService.saveCreateList(xianluList);
            }
            log.info("【t_zhanji】" + "【" + formatter.format(now) + "】");
            List<Zhanji> zhanjiList = zhanjiService.getCreateList();
            if (zhanjiList.size() > 0) {
                zhanjiService.saveCreateList(zhanjiList);
            }
            log.info("【t_cheliang】" + "【" + formatter.format(now) + "】");
            List<Cheliang> cheliangList = cheliangService.getCreateList();
            if (cheliangList.size() > 0) {
                cheliangService.saveCreateList(cheliangList);
            }
            log.info("【t_siji】" + "【" + formatter.format(now) + "】");
            List<Siji> sijiList = sijiService.getCreateList();
            if (sijiList.size() > 0) {
                sijiService.saveCreateList(sijiList);
            }
            log.info("【t_ludan】" + "【" + formatter.format(now) + "】");
            List<Ludan> ludanList = ludanService.getCreateList();
            if (ludanList.size() > 0) {
                ludanService.saveCreateList(ludanList);
            }
            log.info("【t_youhao】" + "【" + formatter.format(now) + "】");
            List<Youhao> youhaoList = youhaoService.getCreateList();
            if (youhaoList.size() > 0) {
                youhaoService.saveCreateList(youhaoList);
            }
            log.info("【t_shike】" + "【" + formatter.format(now) + "】");
            List<Shike> shikeList = shikeService.getCreateList();
            if (shikeList.size() > 0) {
                shikeService.saveCreateList(shikeList);
            }
            log.info("【t_daily】" + "【" + formatter.format(now) + "】");
            List<Daily> dailyList = dailyService.getCreateList();
            if (dailyList.size() > 0) {
                dailyService.saveCreateList(dailyList);
            }
            log.info("【t_rfid】" + "【" + formatter.format(now) + "】");
            List<Rfid> rfidList = rfidService.getCreateList();
            if (rfidList.size() > 0) {
                rfidService.saveCreateList(rfidList);
            }
        } catch (Exception e) {
            log.error("【每天22点定时任务执行结束,执行失败：【{}】", e.getMessage());
        } finally {
            log.info("每天22点定时任务执行结束");
        }
    }
}
